﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Common.UIFunction
{
    public class InputTable
    {
        DataGridView DGV;
        int ColumnCount;
        public InputTable(DataGridView dgv)
        {
            this.DGV = dgv;
            this.ColumnCount = 2;
            this.InitDGV();
        }

        public InputTable(DataGridView dgv, int count)
        {
            this.DGV = dgv;
            this.ColumnCount = count;
            this.InitDGV();
        }

        public void InitDGV()
        {
            DGV.BackgroundColor = System.Drawing.Color.White;
            DGV.AllowUserToAddRows = false;
            DGV.RowHeadersVisible = false;
            DGV.ColumnHeadersVisible = false;
            DGV.AllowUserToResizeColumns = false;
            DGV.AllowUserToResizeRows = false;
        }
        public void InputTableBound(string[] context, Control[] controls, int[] width)
        {
            if (context.Length != controls.Length || width.Length != this.ColumnCount)
                return;

            DGV.Columns.Clear();
            for(int i = 0; i< width.Length; i++)
            {
                DGV.Columns.Add("","");
                DGV.Columns[i].Width = width[i];
            }

            DGV.Rows.Clear();
            int rowcount = (context.Length)/(width.Length/2);
            if ((context.Length) % (width.Length / 2) != 0)
                rowcount++;
            DGV.Rows.Add(rowcount);
           
            this.ResizeDGV();
            for (int rowindex = 0, controlindex = 0; rowindex < DGV.Rows.Count; rowindex++)
            {
                for (int j = 0; j < (width.Length / 2); j++)
                {
                    DGV.Rows[rowindex].Cells[j * 2].Value = context[controlindex];
                    DGV.Rows[rowindex].Cells[j * 2].ReadOnly = true;
                    DGV.Controls.Add(controls[controlindex]);
                    Rectangle rect = DGV.GetCellDisplayRectangle(j*2 + 1, rowindex, false);
                    controls[controlindex].Top = rect.Top + 1;
                    controls[controlindex].Left = rect.Left;
                    controls[controlindex].Width = rect.Width;
                    controls[controlindex].Height = rect.Height;
                    controlindex++;
                    if (controlindex == controls.Length)
                        break;

                }
            }
        }

        public void InputTableBound(Control[] controls, int[] width)
        {
            if (width.Length != this.ColumnCount)
                return;

            DGV.BackgroundColor = System.Drawing.Color.White;
            DGV.AllowUserToAddRows = false;
            DGV.RowHeadersVisible = false;
            DGV.ColumnHeadersVisible = false;
            DGV.AllowUserToResizeColumns = false;
            DGV.AllowUserToResizeRows = false;

            DGV.Columns.Clear();
            for (int i = 0; i < width.Length; i++)
            {
                DGV.Columns.Add("", "");
                DGV.Columns[i].Width = width[i];
            }

            DGV.Rows.Clear();
            int rowcount = (controls.Length) / width.Length;
            if ((controls.Length) % width.Length != 0)
                rowcount++;
            DGV.Rows.Add(rowcount);

            this.ResizeDGV();
            for (int rowindex = 0, controlindex = 0; rowindex < DGV.Rows.Count; rowindex++)
            {
                for (int j = 0; j < width.Length; j++)
                {
                    DGV.Controls.Add(controls[controlindex]);
                    Rectangle rect = DGV.GetCellDisplayRectangle(j, rowindex, false);
                    controls[controlindex].Top = rect.Top - 1;
                    controls[controlindex].Left = rect.Left;
                    controls[controlindex].Width = rect.Width;
                    controls[controlindex].Height = rect.Height;
                    controlindex++;
                    if (controlindex == controls.Length)
                        break;

                }
            }
        }

        public void ResizeDGV()
        {
            DGV.Width = this.DGV.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + 3;
            DGV.Height = this.DGV.Rows.GetRowsHeight(DataGridViewElementStates.Visible) + 3;
        }
        public void AddControlInNewRow(string context, Control newcontrol, int height)
        {
            this.DGV.Rows.Add(1);
            this.DGV.Rows[DGV.Rows.Count - 1].Height = height;
            this.ResizeDGV();
            this.DGV.Rows[DGV.Rows.Count - 1].Cells[0].Value = context;
            DGV.Controls.Add(newcontrol);
            Rectangle rect = DGV.GetCellDisplayRectangle(1, DGV.Rows.Count - 1, false);
            newcontrol.Top = rect.Top;
            newcontrol.Left = rect.Left;
            newcontrol.Height = rect.Height;
            newcontrol.Width = DGV.Right - DGV.GetCellDisplayRectangle(0, DGV.Rows.Count - 1, false).Right - 10; 
        }

    }
}
